<?php
$docroot = dirname(__FILE__);

include_once($docroot.'/class/class.curl.php');

class ajax extends curl
{
    function __construct()
    {
        $url = $_POST['url'];

        $useHeaders = ($_POST['useHeaders']) ? ($requestHeaders = explode("\n", $_POST['requestHeaders'])) : 0;
        $usePost = ($_POST['usePost']) ? ($requestPost = str_replace("\n", "&", $_POST['requestPost'])) : 0;
        $useCookie = ($_POST['useCookie']) ? ($requestCookie = explode("\n", $_POST['requestCookie'])) : 0;

        # QuErY --->

        $result = $this->open($url, $requestHeaders, $requestPost, $requestCookie);

        $headers = mb_substr($result['_response'], 0 , $result['_header_size']);
		$body = mb_substr($result['_response'], $result['_header_size']);

        if($decode = $this->getEncode($headers))
            $body = @mb_convert_encoding($body, 'UTF-8', $decode);

        echo json_encode(array(
            'headers' => $headers,
            'body' => $body,
        ));
    }

    private function getEncode($headers)
    {
        if(preg_match('/charset=(.*)/i', $headers, $decode))
        {
            $decode = str_replace("\r\n", "", $decode['1']);
        }

        return @trim($decode);
    }
}

$ajax = new ajax;

?>
